<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Einstein Class
 */
class CMdDivision extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $div_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $div_code = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $div_name = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $div_address = NULL;
	var $div_tel = NULL;
	var $div_fax = NULL;
	/* sql
	 * ALTER TABLE `divisions` ADD `div_active` INT( 4 ) NOT NULL ;
	 */
	var $div_active = 1;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdDivision() {
		$joins = array(
			array('label'=>'Customer',name=>'companies', 'idfield'=>'company_id', 'joinfield'=>'company_division'),
			array('label'=>'Supplier',name=>'suppliers', 'idfield'=>'sup_id', 'joinfield'=>'sup_division'),
			array('label'=>'Resource',name=>'contacts', 'idfield'=>'contact_id', 'joinfield'=>'contact_division'),
			array('label'=>'Item',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_division'),
			array('label'=>'Task log',name=>'task_log', 'idfield'=>'task_log_id', 'joinfield'=>'task_log_division'),
			array('label'=>'Task',name=>'tasks', 'idfield'=>'task_id', 'joinfield'=>'task_division'),
			array('label'=>'Transportation ',name=>'transportations', 'idfield'=>'trans_id', 'joinfield'=>'trans_division'),
		);
		$this->CDpObject( 'divisions', 'div_id', 'div_code', $joins, 'masterdata' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->div_id");
		$q->addWhere("$this->_tbl_unique = '$this->div_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate division code: ' . $this->div_code;
		}
		return NULL;
	}
}
function getDivisionList($required=true){
	$result = array('div_id' => 0, 'div_name' => '');
	$q  = new DBQuery;
	$q->addTable('divisions');
	$q->addQuery('div_id, div_name');
	$q->addWhere('div_active = 1');
	$sql = $q->prepare();
	$q->clear();
	//db_loadHash($sql, $result);
	$results = db_loadHashList($sql);
	if (!$required) {
		return arrayMerge(array('' => ''), $results);
	}
	return $results;
}
?>
